package com.ruoyix.ai.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyix.ai.domain.AiChatMessage;
import com.ruoyix.ai.domain.AiChatMessageSendReqVO;
import com.ruoyix.ai.domain.AiChatMessageSendRespVO;
import com.ruoyix.common.helper.LoginHelper;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.utils.poi.ExcelUtil;
import com.ruoyix.ai.domain.vo.AiChatMessageVo;
import com.ruoyix.ai.domain.bo.AiChatMessageBo;
import com.ruoyix.ai.service.IAiChatMessageService;
import com.ruoyix.common.core.page.TableDataInfo;
import reactor.core.publisher.Flux;

/**
 * AI 聊天消息
 *
 * @author ruoyi
 * @date 2024-09-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/aiChatMessage")
public class AiChatMessageController extends BaseController {

    private final IAiChatMessageService iAiChatMessageService;

    /**
     * 查询AI 聊天消息列表
     */
    @SaCheckPermission("ai:aiChatMessage:list")
    @GetMapping("/list")
    public TableDataInfo<AiChatMessageVo> list(AiChatMessageBo bo, PageQuery pageQuery) {
        return iAiChatMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AI 聊天消息列表
     */
    @SaCheckPermission("ai:aiChatMessage:export")
    @Log(title = "AI 聊天消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiChatMessageBo bo, HttpServletResponse response) {
        List<AiChatMessageVo> list = iAiChatMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "AI 聊天消息", AiChatMessageVo.class, response);
    }

    /**
     * 获取AI 聊天消息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ai:aiChatMessage:query")
    @GetMapping("/{id}")
    public R<AiChatMessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iAiChatMessageService.queryById(id));
    }

    /**
     * 新增AI 聊天消息
     */
    @SaCheckPermission("ai:aiChatMessage:add")
    @Log(title = "AI 聊天消息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiChatMessageBo bo) {
        return toAjax(iAiChatMessageService.insertByBo(bo));
    }

    /**
     * 修改AI 聊天消息
     */
    @SaCheckPermission("ai:aiChatMessage:edit")
    @Log(title = "AI 聊天消息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiChatMessageBo bo) {
        return toAjax(iAiChatMessageService.updateByBo(bo));
    }

    /**
     * 删除AI 聊天消息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ai:aiChatMessage:remove")
    @Log(title = "AI 聊天消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAiChatMessageService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    @Operation(summary = "发送消息（流式）", description = "流式返回，响应较快")
    @PostMapping(value = "/send-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @PermitAll // 解决 SSE 最终响应的时候，会被 Access Denied 拦截的问题
    public Flux<R<AiChatMessageSendRespVO>> sendChatMessageStream(@Valid @RequestBody AiChatMessageSendReqVO sendReqVO) {
        return iAiChatMessageService.sendChatMessageStream(sendReqVO, LoginHelper.getUserId());
    }


    @Operation(summary = "发送消息（流式）", description = "流式返回，响应较快")
    @PostMapping(value = "/send-call")
    @PermitAll // 解决 SSE 最终响应的时候，会被 Access Denied 拦截的问题
    public R<AiChatMessage> sendChatMessageCall(@Valid @RequestBody AiChatMessageSendReqVO sendReqVO) {
        return R.ok(iAiChatMessageService.sendChatMessageCall(sendReqVO, LoginHelper.getUserId()));
    }
}
